package cn.edu.thu.tsquality.core.common.table;

import cn.edu.thu.tsquality.core.common.datasource.IDataSource;

import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Created by wangyihan  on 2018/4/3 下午3:48. E-mail address is yihanwang22@163.com. Copyright © 2017
 * wangyihan. All Rights Reserved.
 *
 * @author wangyihan
 */
public class StreamingTable {

  private IDataSource dataSource;
  private Header header;
  private int pos;

  public StreamingTable(IDataSource dataSource, Header header) {
    this.dataSource = dataSource;
    this.header = header;
    this.pos = 0;
  }

  public Header getHeader() {
    return header;
  }

  public boolean hasNext() {
    return dataSource.hasNext();
  }

  public Row nextRow() {
    if (!hasNext()) {
      throw new NoSuchElementException("No more elements");
    }
    try {
      return dataSource.readRow();
    } catch (IOException e) {
      throw new NoSuchElementException("Cannot read next row from datasource");
    }
  }

  public Iterator<Row> iterator() {
    return new TableIterator(this);
  }
}
